<html>
<head><meta charset="utf-8"><title>Can I make an `impl Add&lt;..&gt; for MyType` panic in `std`? · t-libs/api-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/265478-t-libs/api-guidelines/index.html">t-libs/api-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/265478-t-libs/api-guidelines/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html">Can I make an `impl Add&lt;..&gt; for MyType` panic in `std`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228429889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/265478-t-libs/api-guidelines/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228429889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/265478-t-libs/api-guidelines/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228429889">(Mar 02 2021 at 15:35)</a>:</h4>
<p>Hello, I'm thinking about <code>impl Add&lt;u*&gt; for NonZeroU*</code> in <code>std</code>.<br>
This could be safe in debug mode because <code>Add</code> panics on overflow, but it would be unsafe in release mode because the nonzero invariant could be broken on overflow.<br>
Is it okay that to make <code>impl Add&lt;u*&gt; for NonZeroU*</code> panic <em>also</em> in release mode instead? Or is it something an <code>impl Add</code> should explicitely never do?</p>



<a name="228448814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/265478-t-libs/api-guidelines/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228448814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/265478-t-libs/api-guidelines/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228448814">(Mar 02 2021 at 17:21)</a>:</h4>
<p>I don't think there's any rule against panics. Some ops may still panic in release mode, like <code>Div</code> or <code>Rem</code> by zero.</p>



<a name="228449093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/265478-t-libs/api-guidelines/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228449093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/265478-t-libs/api-guidelines/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228449093">(Mar 02 2021 at 17:22)</a>:</h4>
<p>The overflow wrapping behavior is mostly for performance. If there were a faster way to have checked ops, Rust might have chosen that instead.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>